package ${table.actionPackageName};

import ${table.servicePackageName}.${table.serviceName};
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import ${table.entityPackageName}.${table.entityName};
import tf56.ehuodi.domain.ResultPojo;
import tf56.ehuodi.utils.RequestUtils;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import tf56.ehuodiOperate.utils.NumberUtils;
import tf56.huilian.services.LockService;

/**
* ${table.actionName}
* ${table.tableComment}
* @author  ${config.author}
* @version ${config.version}
* 描述：${config.description}
* 时间：${table.genDateTime?string("yyyy-MM-dd HH:mm:ss")}
*/
@RestController
@RequestMapping("${table.actionRequestMapping}")
public class ${table.actionName}{

private static final Logger LOGGER = LoggerFactory.getLogger(${table.actionName}.class);

private static String insert_lock_key = "${table.actionName}_lock_insert${table.entityName}";

@Autowired
private LockService lockService;

@Autowired
private ${table.serviceName} ${table.serviceFirstLowerName};


/******************************开发的代码区域 开始***************************/


/******************************开发的代码区域 结束***************************/


/***************************自动生成的代码区域 开始**************************/
<#assign num=0 />
<#list columns as column>
        <#if column.name == 'updateJobcard' || column.name == 'updateUsername'>
                <#assign num=num+1 />
        </#if>
</#list>

/**
* 新增 ${table.tableComment}
* @param ${table.entityFirstLowerName}
* @return
*/
@RequestMapping(value = "/insert",method= RequestMethod.POST)
public String insert(HttpServletRequest req, @Valid ${table.entityName} ${table.entityFirstLowerName}) {
LOGGER.info("新增 ${table.tableComment} 入参:{}",${table.entityFirstLowerName});
if(${table.entityFirstLowerName}.get${table.primaryColumn.nameTopBig}()!=null){
return ResultPojo.Err("id不可传入").toJson();
}
if (!lockService.lock(insert_lock_key)) {
return ResultPojo.Err("当前已有操作进行中").toJson();
}
try{
ResultPojo res = ${table.serviceFirstLowerName}.insert(${table.entityFirstLowerName});
return res.toJson();
}catch(Exception e){
LOGGER.info("${table.tableComment} 新增 失败 :",e);
return ResultPojo.Err("创建失败").toJson();
}finally {
lockService.unlock(insert_lock_key);
}
}

/**
* 删除 ${table.tableComment}
* @param id
* @return
*/
@RequestMapping(value = "/delete",method= RequestMethod.POST)
public String delete(String ${table.primaryColumn.name}) {
LOGGER.info("删除 ${table.tableComment} 入参:{}",${table.primaryColumn.name});
ResultPojo res = ${table.serviceFirstLowerName}.delete(${table.primaryColumn.name});
return res.toJson();
}

/**
* 编辑 ${table.tableComment}
* @param ${table.entityFirstLowerName}
* @return
*/
@RequestMapping(value = "/update",method= RequestMethod.POST)
public String update(HttpServletRequest req,@Valid ${table.entityName} ${table.entityFirstLowerName}) {
LOGGER.info("修改 ${table.tableComment} 入参:{}",${table.entityFirstLowerName});
<#if num == 2>
        setUser(req,${table.entityFirstLowerName}); //写入 操作用户
</#if>
ResultPojo
<Integer> res = ${table.serviceFirstLowerName}.update(${table.entityFirstLowerName});
        return res.toJson();
        }

        /**
        * 通用列表查询 ${table.tableComment}
        * @param HttpServletRequest
        * @return
        */
        @RequestMapping(value = "/selectList",method= RequestMethod.POST)
        public String selectList(HttpServletRequest request) {
        Map
        <String
                , String> params = RequestUtils.getParamMapFromRequest(request);
                return ${table.serviceFirstLowerName}.selectList(params);
                }

                /**
                * 通用单体查询 ${table.tableComment}
                * @param ${table.primaryColumn.name}
                * @return
                */
                @RequestMapping(value = "/selectOne",method= RequestMethod.POST)
                public String selectOne(String ${table.primaryColumn.name}) {
                return ${table.serviceFirstLowerName}.selectOne(${table.primaryColumn.name});
                }

                <#if num == 2>
                        private void setUser(HttpServletRequest req,${table.entityName} ${table.entityFirstLowerName}){
                        String username = (String) req.getParameter("username");
                        String jobCard = (String) req.getParameter("jobCard");
                        ${table.entityFirstLowerName}.setUpdateJobcard(jobCard);
                        ${table.entityFirstLowerName}.setUpdateUsername(username);
                        }
                </#if>
}
                /***************************自动生成的代码区域 结束**************************/
}